package com.jm.cloud.feign;

import com.jm.cloud.feign.exception.FeignExceptionDecoder;
import com.jm.cloud.feign.result.FeignResultDecoder;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import feign.optionals.OptionalDecoder;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * tide-潮汐
 * <p>
 * Feign 自动配置
 * <p>
 * author: jiangjianmin
 * <p>
 * create: 2024-08-22 15:05
 **/
@Configuration
@EnableFeignClients(basePackages = "com.jm.**.feign")
public class FeignAutoConfig {
    @Bean
    public Decoder feignDecoder(ObjectProvider<HttpMessageConverters> messageConverters) {
        // 返回一个OptionalDecoder实例，用于处理Feign调用中的解码逻辑
        // 其中，ResponseEntityDecoder用于将API响应解码为Java对象
        // SpringDecoder基于提供的HttpMessageConverters进行实际的解码工作
        return new OptionalDecoder((new ResponseEntityDecoder(new FeignResultDecoder(new SpringDecoder(messageConverters)))));
    }


    @Bean
    public ErrorDecoder feignErrorDecoder(ObjectProvider<HttpMessageConverters> messageConverters) {
        return new FeignExceptionDecoder(new SpringDecoder(messageConverters));
    }
}
